<?php
namespace app\manage\controller;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\Db;
class Excel extends Base
{
    public function index() {
        try {
            $list = Db::table('kt_user')->field('id,nickname')->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $title = '年收费统计表';
        $sheet->setTitle($title);
        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '昵称');
        $sheet->getColumnDimension('A')->setWidth(12);
        $sheet->getColumnDimension('B')->setWidth(20);

        $index = 2;
        foreach ($list as $v) {
            $sheet->setCellValue('A'.$index, $v['id']);
            $sheet->setCellValueExplicit('B'.$index, $v['nickname'],DataType::TYPE_STRING);
            $index++;
        }
        $writer = new Xlsx($spreadsheet);
        $filename = date('YmdHis') . '.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
        header('Content-Disposition: attachment;filename=' . $filename);//告诉浏览器输出浏览器名称
        header('Cache-Control: max-age=0');//禁止缓存
        $writer->save('php://output');
        exit('success');
    }

    public function test() {
        $post['nickname'] = input('post.nickname','');
        $id = Db::table('kt_user')->insertGetId($post);
        halt($id);
    }





}